<?php
	include_once('classes/view.php');
	View::displayHeader();
	
	if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != ''){
		include_once('classes/functionDb.php');
		
		try {
			
			$MyQuery = "SELECT * FROM equipment WHERE user_id=" . $_SESSION['user_id'];
			$MyConn	 = ConnectUser();
			$MyResult = $MyConn->query($MyQuery);
			
			mysqli_close($MyConn);
			if(!$MyResult) Throw New Exception("Nie udało się pobrać urządzeń.");

			DisplayUserDevice($MyResult->fetch_row());
		} catch (Exception $ex){
			View::displayMiddle($ex->getMessage());
		}
	} else {
		View::displayMiddle("Musisz być zarejestrowany i zalogowany aby tutaj wejść");
	}
	View::displayFooter();
	die();
	
	function DisplayUserDevice($pUserData){
		include_once('classes/devices.php');
			
		$MyObject			 	= new Devices();
		$MyGraphicList 		 	= $MyObject -> GetGraphics();
		$MyProcessorList	 	= $MyObject -> GetProcessors();
		$MyMotherboardList		= $MyObject -> GetMotherBoards();
		$MyMemoryList			= $MyObject -> GetMemory();
		$MyDiskList				= $MyObject -> GetDisks();
		
		echo '<script type="text/javascript">
				function showDevice(pId, pField, pDevice) {
					if (pId==""){
					  	document.getElementById(pField).innerHTML="";
					  	return;
					}
					
					if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari
						xmlhttp=new XMLHttpRequest();
					} else {// code for IE6, IE5
					  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					
					xmlhttp.onreadystatechange=function() {
						if (xmlhttp.readyState==4 && xmlhttp.status==200){
					    	document.getElementById(pField).innerHTML=xmlhttp.responseText;
					    }
					}
					
					xmlhttp.open("GET","compare_get_user.php?kind_device="+pDevice+"&dev_id="+pId,true);
					xmlhttp.send();
				}
				</script>';
		
		echo '<form method="post" action="user_update_device.php">
			<div class="box">
			<h1>Informacje dotyczące sprzętu</h1>
			<br />
			<table border="0">';

		displayColumn($MyGraphicList, "gpu", "graphic", "Karta graficzna: ", $pUserData[5]);
		displayColumn($MyProcessorList, "cpu", "processor", "Procesor: ", $pUserData[3]);
		displayColumn($MyMotherboardList, "motherboard", "motherboard", "Płyta główna: ", $pUserData[2]);
		displayColumn($MyMemoryList, "mem", "memory", "Pamięć: ", $pUserData[4]);
		displayColumn($MyDiskList, "disk", "disk", "Dysk twardy: ", $pUserData[6]);
		
		echo '
			<tr>
				<td><input class="button" type="submit" name="Submit" value="Aktualizuj" /></td>
			</tr>
		';
		echo '</table></div></form><br /><br /><br /><br />';
	}
	
	function displayColumn($pList, $pField, $pDevice, $pText, $pUserDeviceId){
		echo '
			<tr>
				<td rowspan="2" width="200"><i>' . $pText . '</i></td>
				<td>
		';
		echo '
		  <select class="defaultText" name=\'' . $pDevice . '\' onchange="showDevice(this.value, \'' . $pField . '\', \'' . $pDevice . '\')">
		  <option value=\'\'>Proszę wybrać coś z listy</option>';
	
		foreach($pList as $id => $name){
			if ($pUserDeviceId == $id){
				echo '<option selected="selected" value=\'' . $id . '\'> ' . $name . '</option>';
			} else {
				echo '<option value=\'' . $id . '\'> ' . $name . '</option>';
			}
		}
	
		echo '</select>
			  </td></tr>
			  <tr><td>
				<div id=\'' . $pField . '\'></div>
			  </td></tr>';
	
	}
?>